# 灰度化的通道会减为1
# 平均值灰度化 （红 绿 蓝 取平均值赋给灰度图）
import cv2
import numpy as np
from time import time

img = cv2.imread('D:/python/opencv-processing/laiya.jpg')
# 取出图像的高，宽，深度 depth（通道数量）
h, w, c = img.shape
start = time()
# 一个通道的灰度图
gray = np.zeros((h, w), dtype=np.uint8)
img = img / 255
# for i in range(h):
#     for j in range(w):
        # gray[i, j] = ((img[i, j, 0] + img[i, j, 1] + img[i, j, 2]) / 3 *255)


# 高效率方法
# img = img / 255
# 最优方法 0.02161884307861328
gray = ((img[:, :, 0] + img[:, :, 1] + img[:, :, 2]) / 3 *255).astype(np.uint8)
# 最差方法
# for i in range(h):
#     for j in range(w):
        # gray[i, j] = np.mean(img[i, j])
# gray = (np.mean(img,axis=2)*255).astype(np.uint8)

end = time()
print(end - start)

# 均值灰度化
cv2.imshow("gray", gray)
cv2.waitKey(0)
